﻿#include "CustomListModel.h"
#include <QDebug>
#include <QList>
#include <QMap>

CustomListModel::CustomListModel(QObject *parent)
    : QAbstractListModel(parent)
{

}

CustomListModel::~CustomListModel()
{

}

int CustomListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return m_dataList.count();
}

QVariant CustomListModel::data(const QModelIndex &index, int role) const
{
    const DataItem &item = m_dataList.at(index.row());

    switch (role) {
    case Qt::DisplayRole:
    case Qt::EditRole:
        switch (item.type) {
        case ComboType: {
            ComboData combo = item.metaData.value<ComboData>();
            return combo.items.value(combo.currentIndex, "");
        }
        default:
            return item.value;
        }
    case Qt::TextAlignmentRole:
        return Qt::AlignCenter;
         // 返回数据类型
    case Qt::UserRole:
        return QVariant::fromValue(item.type);
        // 返回元数据
    case Qt::UserRole + 1:
        return item.metaData;
    default:
        return QVariant();
    }
}

bool CustomListModel::setData(const QModelIndex &index, const QVariant &userData, int role)
{
    if (!index.isValid() || index.row() >= m_dataList.size() || role != Qt::EditRole)
        return false;

    DataItem &item = m_dataList[index.row()];

    switch (item.type) {
    case IntType:
        item.value = userData.toInt();
        break;
    case ComboType: {
        ComboData combo = item.metaData.value<ComboData>();
        combo.currentIndex = userData.toInt();
        item.metaData = QVariant::fromValue(combo);
        break;
    }
    case BoolType:
        item.value = userData.toBool();
        break;
    default:
        item.value = userData.toString();
    }

    emit dataChanged(index, index);
    return true;
}

Qt::ItemFlags CustomListModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

bool CustomListModel::insertRows(int row, int count, const QModelIndex &parent)
{
    if (row < 0 || row > m_dataList.size() || count <= 0)
        return false;

    beginInsertRows(parent, row, row + count - 1);
    for (int i = 0; i < count; ++i) {
        DataItem item;
        item.type = StringType;
        item.value = "新项目";
        m_dataList.insert(row, item);
    }
    endInsertRows();
    return true;
}

bool CustomListModel::insertRows(int row, int count, const DataItem& userData, const QModelIndex &parent)
{
    if (row < 0 || row > m_dataList.size() || count <= 0)
        return false;

    beginInsertRows(parent, row, row + count - 1);
    for (int i = 0; i < count; ++i) {
        m_dataList.insert(row, userData);
    }
    endInsertRows();
    return true;
}

bool CustomListModel::insertRows(int row, const DataItem& userData, const QModelIndex &parent)
{
    if (row < 0 || row > m_dataList.size())
        return false;

    beginInsertRows(parent, row, row);
    m_dataList.insert(row, userData);
    endInsertRows();
    return true;
}

bool CustomListModel::addItem(const DataItem& userData)
{
    return insertRows(m_dataList.size(), userData);
}

bool CustomListModel::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    for (int i = 0; i < count; ++i)
        m_dataList.removeAt(row);
    endRemoveRows();
    return true;
}

bool CustomListModel::removeItem(int row)
{
    if (row < 0 || row >= m_dataList.size())
        return false;

    beginRemoveRows(QModelIndex(), row, row);
    m_dataList.removeAt(row);
    endRemoveRows();
    return true;
}

bool CustomListModel::clearAll()
{
    beginResetModel();
    m_dataList.clear();
    endResetModel();
    return true;
}
